package main

import (
	"flag"
	"fmt"
	"math"
	"strconv"
	"strings"
)

//作业4 接收两个坐标点的输入，并计算两个坐标点之间的距离（需要查询 math 开根号函数）
func main() {

	var SXY string
	var DXY string
	flag.StringVar(&SXY, "s", "0,0", "源坐标 -s :格式为 0,0")
	flag.StringVar(&DXY, "d", "0,0", "目的坐标 -d :格式为 0,0")
	flag.Parse()

	var s = NewCoordsByXYsting(SXY)
	var d = NewCoordsByXYsting(DXY)

	fmt.Print(s.XYstring, "~", d.XYstring)
	fmt.Println("两点之间的距离为", GetRrlen(s, d))

}

//坐标结构体
type Coords struct {
	Xcode    float64
	Ycode    float64
	XYstring string
}

//通过输入的坐标初始化结构体
func NewCoordsByXYsting(XYstring string) Coords {
	var XYarr = strings.Split(XYstring, ",")
	var Xcode float64
	var Ycode float64
	Xcode, _ = strconv.ParseFloat(XYarr[0], 64)
	Ycode, _ = strconv.ParseFloat(XYarr[1], 64)

	return Coords{
		Xcode:    Xcode,
		Ycode:    Ycode,
		XYstring: XYstring,
	}
}

//计算距离
func GetLen(x float64, y float64) float64 {

	x = x * x
	y = y * y
	var res float64
	res = math.Sqrt(x + y)
	return res
}

//通过坐标获取真实距离
func GetRrlen(SXY Coords, DXY Coords) float64 {
	var res float64
	var x float64
	var y float64
	x = math.Abs(SXY.Xcode - DXY.Xcode)
	y = math.Abs(SXY.Ycode - DXY.Ycode)
	res = GetLen(x, y)
	return res
}
